package com.cinc.ebc.fi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cinc.ebc.fi.constant.*;
import com.cinc.ebc.fi.entity.FiApOriginalAmount;
import com.cinc.ebc.fi.entity.FiOtherPaymentDetail;
import com.cinc.ebc.fi.entity.FiPaymentVoucher;
import com.cinc.ebc.fi.entity.FiPaymentVoucherDetail;
import com.cinc.ebc.fi.mapper.*;
import com.cinc.ebc.fi.model.FiPaymentVoucherDetailModel;
import com.cinc.ebc.fi.model.FiSupplyPaymentDetailModel;
import com.cinc.ebc.fi.model.FiSupplyPrePaymentDetailModel;
import com.cinc.ebc.fi.service.IFiPaymentVoucherService;
import com.cinc.ebc.fi.vo.FiPaymentVoucherDetailPage;
import com.cinc.ebc.fi.vo.FiSupplyPaymentDetailPage;
import com.cinc.ebc.mm.constant.StockInOutDetailVerifyStatus;
import com.cinc.ebc.mm.entity.MmStockInOutDetail;
import com.cinc.ebc.mm.mapper.MmStockInOutDetailMapper;
import com.cinc.mdm.modules.codeRule.service.IMdmCodeRuleService;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 付款单
 * @Author: jeecg-boot
 * @Date:   2022-12-17
 * @Version: V1.0
 */
@Service
public class FiPaymentVoucherServiceImpl extends ServiceImpl<FiPaymentVoucherMapper, FiPaymentVoucher> implements IFiPaymentVoucherService {

	@Autowired
	private FiPaymentVoucherMapper fiPaymentVoucherMapper;
	@Autowired
	private FiPaymentVoucherDetailMapper fiPaymentVoucherDetailMapper;

	@Autowired
	private IMdmCodeRuleService mdmCodeRuleService;

	@Autowired
	private MmStockInOutDetailMapper mmStockInOutDetailMapper;

	@Autowired
	private FiApOriginalAmountMapper fiApOriginalAmountMapper;

	@Autowired
	private FiOtherPaymentDetailMapper fiOtherPaymentDetailMapper;

	@Autowired
	private FiOtherPaymentMapper fiOtherPaymentMapper;

	@Override
	@Transactional
	public void saveMain(FiPaymentVoucher fiPaymentVoucher, List<FiPaymentVoucherDetail> fiPaymentVoucherDetailList) {
		BigDecimal actAmount = fiPaymentVoucher.getActAmount() == null ? BigDecimal.ZERO : fiPaymentVoucher.getActAmount();
		BigDecimal verifyAmount = fiPaymentVoucher.getVerifyAmount() == null ? BigDecimal.ZERO : fiPaymentVoucher.getVerifyAmount();
		if(PaymentVoucherType.TYPE_01.equals(fiPaymentVoucher.getVoucherType())){
			if(actAmount.compareTo(verifyAmount) == -1){
				throw new RuntimeException("实付金额不能小于核销金额");
			}
			// 预付款处理
			BigDecimal preAmount = actAmount.subtract(verifyAmount);
			if(preAmount.compareTo(BigDecimal.ZERO)== 1){
				fiPaymentVoucher.setActAmount(verifyAmount);
				// 实付金额超出部分，转为预付款
				FiPaymentVoucher fiPaymentVoucherPre = new FiPaymentVoucher();
				fiPaymentVoucherPre.setVoucherCode(mdmCodeRuleService.generateFiPaymentVoucherCode());
				fiPaymentVoucherPre.setVoucherDate(fiPaymentVoucher.getVoucherDate());
				fiPaymentVoucherPre.setVoucherType(PaymentVoucherType.TYPE_02);
				fiPaymentVoucherPre.setCurrency(fiPaymentVoucher.getCurrency());
				fiPaymentVoucherPre.setSupplyId(fiPaymentVoucher.getSupplyId());
				fiPaymentVoucherPre.setSettleAccountId(fiPaymentVoucher.getSettleAccountId());
				fiPaymentVoucherPre.setActAmount(preAmount);
				fiPaymentVoucherPre.setStatus(AparVoucherStatus.STATUS_01);
				fiPaymentVoucherPre.setMaker(fiPaymentVoucher.getMaker());
				fiPaymentVoucherPre.setUnverifyAmount(preAmount);
				fiPaymentVoucherMapper.insert(fiPaymentVoucherPre);
			}
		}else{
			if(verifyAmount.compareTo(BigDecimal.ZERO) != 0 && actAmount.compareTo(verifyAmount) != 0){
				throw new RuntimeException("实付金额与核销金额不符，请修改");
			}
		}
		fiPaymentVoucher.setUnverifyAmount(fiPaymentVoucher.getActAmount().subtract(fiPaymentVoucher.getVerifyAmount()));
		fiPaymentVoucherMapper.insert(fiPaymentVoucher);

		//
		if(fiPaymentVoucherDetailList!=null && fiPaymentVoucherDetailList.size()>0) {
			for(FiPaymentVoucherDetail entity:fiPaymentVoucherDetailList) {
				//外键设置
				entity.setMainId(fiPaymentVoucher.getId());
				BigDecimal detailVerifyAmount = entity.getVerifyAmount() == null ? BigDecimal.ZERO : entity.getVerifyAmount();
				if(detailVerifyAmount.compareTo(BigDecimal.ZERO) == -1){
					throw new RuntimeException("核销金额不能小于零");
				}
				// 业务单据处理
				handleVoucherDetail(entity);
				// 新增
				fiPaymentVoucherDetailMapper.insert(entity);
			}
		}
	}

	/**
	 * 处理业务单据
	 * @param entity
	 */
	private void handleVoucherDetail(FiPaymentVoucherDetail entity){
		FiPaymentVoucherDetail entityHis = fiPaymentVoucherDetailMapper.selectById(entity.getId());
		if(AparVoucherDetailBillType.AP_01.equals(entity.getBillType())) {
			//采购入库单
			MmStockInOutDetail stockInOutDetail = mmStockInOutDetailMapper.selectById(entity.getBillId());
			if (stockInOutDetail.getVerifyStatus().equals(StockInOutDetailVerifyStatus.STATUS_03)) {
				throw new RuntimeException("已核销单据，不能付款");
			}
			BigDecimal sumPrice = stockInOutDetail.getSumPrice() == null ? BigDecimal.ZERO : stockInOutDetail.getSumPrice();
			BigDecimal stockVerifyAmount = stockInOutDetail.getVerifyAmount() == null ? BigDecimal.ZERO : stockInOutDetail.getVerifyAmount();
			BigDecimal verifyAmount = entity.getVerifyAmount() == null ? BigDecimal.ZERO : entity.getVerifyAmount();
			BigDecimal finalAmount = BigDecimal.ZERO;
			if (entityHis == null) {
				finalAmount = verifyAmount.add(stockVerifyAmount);
			} else {
				BigDecimal verifyAmountHis = entityHis.getVerifyAmount() == null ? BigDecimal.ZERO : entityHis.getVerifyAmount();
				finalAmount = stockVerifyAmount.subtract(verifyAmountHis).add(verifyAmount);
			}
			if (finalAmount.compareTo(sumPrice) == 1) {
				throw new RuntimeException("核销金额不能大于单据金额");
			}
			MmStockInOutDetail stockInOutDetailModify = new MmStockInOutDetail();
			stockInOutDetailModify.setId(entity.getBillId());
			stockInOutDetailModify.setVerifyDate(new Date());
			stockInOutDetailModify.setVerifyAmount(finalAmount);
			mmStockInOutDetailMapper.updateById(stockInOutDetailModify);
		}else if(AparVoucherDetailBillType.AP_04.equals(entity.getBillType())) {
			// 应付期初余额
			FiApOriginalAmount fiApOriginalAmount = fiApOriginalAmountMapper.selectById(entity.getBillId());
			if (fiApOriginalAmount.getSettleStatus().equals(AparSettleStatus.STATUS_30)) {
				throw new RuntimeException("期初已结算完成，不能付款");
			}
			BigDecimal initialBalance = fiApOriginalAmount.getInitialBalance() == null ? BigDecimal.ZERO : fiApOriginalAmount.getInitialBalance();
			BigDecimal originalVerifyAmount = fiApOriginalAmount.getVerifyAmount() == null ? BigDecimal.ZERO : fiApOriginalAmount.getVerifyAmount();
			BigDecimal verifyAmount = entity.getVerifyAmount() == null ? BigDecimal.ZERO : entity.getVerifyAmount();
			BigDecimal finalAmount = BigDecimal.ZERO;
			if (entityHis == null) {
				finalAmount = verifyAmount.add(originalVerifyAmount);
			} else {
				BigDecimal verifyAmountHis = entityHis.getVerifyAmount() == null ? BigDecimal.ZERO : entityHis.getVerifyAmount();
				finalAmount = originalVerifyAmount.subtract(verifyAmountHis).add(verifyAmount);
			}
			if (finalAmount.compareTo(initialBalance) == 1) {
				throw new RuntimeException("核销金额不能大于期初余额");
			}
			FiApOriginalAmount fiApOriginalAmountModify = new FiApOriginalAmount();
			fiApOriginalAmountModify.setId(entity.getBillId());
			fiApOriginalAmountModify.setVerifyAmount(finalAmount);
			fiApOriginalAmountMapper.updateById(fiApOriginalAmountModify);
		}else if(AparVoucherDetailBillType.AP_03.equals(entity.getBillType())) {
			// 其他应付单
			FiOtherPaymentDetail fiOtherPaymentDetail = fiOtherPaymentDetailMapper.selectById(entity.getBillId());
			if (fiOtherPaymentDetail.getStatus().equals(AparOtherDetailStatus.STATUS_03)) {
				throw new RuntimeException("已完成单据，不能付款");
			}
			BigDecimal otherPaymentAmount = fiOtherPaymentDetail.getPaymentAmount() == null ? BigDecimal.ZERO : fiOtherPaymentDetail.getPaymentAmount();
			BigDecimal otherVerifyAmount = fiOtherPaymentDetail.getVerifyAmount() == null ? BigDecimal.ZERO : fiOtherPaymentDetail.getVerifyAmount();
			BigDecimal verifyAmount = entity.getVerifyAmount() == null ? BigDecimal.ZERO : entity.getVerifyAmount();
			BigDecimal finalAmount = BigDecimal.ZERO;
			if (entityHis == null) {
				finalAmount = verifyAmount.add(otherVerifyAmount);
			} else {
				BigDecimal verifyAmountHis = entityHis.getVerifyAmount() == null ? BigDecimal.ZERO : entityHis.getVerifyAmount();
				finalAmount = otherVerifyAmount.subtract(verifyAmountHis).add(verifyAmount);
			}
			if (finalAmount.compareTo(otherPaymentAmount) == 1) {
				throw new RuntimeException("核销金额不能大于单据金额");
			}
			FiOtherPaymentDetail fiOtherPaymentDetailModify = new FiOtherPaymentDetail();
			fiOtherPaymentDetailModify.setId(entity.getBillId());
			fiOtherPaymentDetailModify.setVerifyAmount(finalAmount);
			fiOtherPaymentDetailMapper.updateById(fiOtherPaymentDetailModify);
			// 更新主表核销金额
			fiOtherPaymentMapper.autoUpdateVerifyAmount(fiOtherPaymentDetail.getMainId());
		}
	}

	@Override
	@Transactional
	public void updateMain(FiPaymentVoucher fiPaymentVoucher,List<FiPaymentVoucherDetail> fiPaymentVoucherDetailList) {
		FiPaymentVoucher fiPaymentVoucherHis = fiPaymentVoucherMapper.selectById(fiPaymentVoucher.getId());
		if(AparVoucherStatus.STATUS_03.equals(fiPaymentVoucherHis.getStatus())){
			throw new RuntimeException("已经核销完成，不能修改");
		}
		BigDecimal mainActAmount = fiPaymentVoucher.getActAmount() == null ? BigDecimal.ZERO : fiPaymentVoucher.getActAmount();
		BigDecimal mainVerifyAmount = fiPaymentVoucher.getVerifyAmount() == null ? BigDecimal.ZERO : fiPaymentVoucher.getVerifyAmount();
		if(PaymentVoucherType.TYPE_01.equals(fiPaymentVoucher.getVoucherType())){
			if(AparVoucherStatus.STATUS_02.equals(fiPaymentVoucherHis.getStatus())){
				throw new RuntimeException("已经审核，不能修改");
			}
			if(mainActAmount.compareTo(mainVerifyAmount) == -1){
				throw new RuntimeException("实付金额不能小于核销金额");
			}
			// 预付款处理
			BigDecimal preAmount = mainActAmount.subtract(mainVerifyAmount);
			if(preAmount.compareTo(BigDecimal.ZERO)== 1){
				fiPaymentVoucher.setActAmount(mainVerifyAmount);
				// 实付金额超出部分，转为预付款
				FiPaymentVoucher fiPaymentVoucherPre = new FiPaymentVoucher();
				fiPaymentVoucherPre.setVoucherCode(mdmCodeRuleService.generateFiPaymentVoucherCode());
				fiPaymentVoucherPre.setVoucherDate(fiPaymentVoucher.getVoucherDate());
				fiPaymentVoucherPre.setVoucherType(PaymentVoucherType.TYPE_02);
				fiPaymentVoucherPre.setCurrency(fiPaymentVoucher.getCurrency());
				fiPaymentVoucherPre.setSupplyId(fiPaymentVoucher.getSupplyId());
				fiPaymentVoucherPre.setSettleAccountId(fiPaymentVoucher.getSettleAccountId());
				fiPaymentVoucherPre.setActAmount(preAmount);
				fiPaymentVoucherPre.setStatus(AparVoucherStatus.STATUS_01);
				fiPaymentVoucherPre.setMaker(fiPaymentVoucher.getMaker());
				fiPaymentVoucherPre.setUnverifyAmount(preAmount);
				fiPaymentVoucherMapper.insert(fiPaymentVoucherPre);
			}
		}else{
			BigDecimal mainActAmountHis = fiPaymentVoucherHis.getActAmount() == null ? BigDecimal.ZERO : fiPaymentVoucherHis.getActAmount();
			if(AparVoucherStatus.STATUS_02.equals(fiPaymentVoucherHis.getStatus()) && mainActAmount.compareTo(mainActAmountHis) != 0){
				throw new RuntimeException("已经审核，不能修改实付金额");
			}
			if(mainVerifyAmount.compareTo(BigDecimal.ZERO) != 0 && mainActAmount.compareTo(mainVerifyAmount) != 0){
				throw new RuntimeException("实付金额与核销金额不符，请修改");
			}
		}
		fiPaymentVoucher.setUnverifyAmount(fiPaymentVoucher.getActAmount().subtract(fiPaymentVoucher.getVerifyAmount()));
		fiPaymentVoucherMapper.updateById(fiPaymentVoucher);
		// 获取子表数据
		LambdaQueryWrapper<FiPaymentVoucherDetail> detailQuery = new LambdaQueryWrapper<>();
		detailQuery.eq(FiPaymentVoucherDetail::getMainId, fiPaymentVoucher.getId());
		List<FiPaymentVoucherDetail> paymentVoucherDetailHisList = fiPaymentVoucherDetailMapper.selectList(detailQuery);
		List<String> detailIdList = paymentVoucherDetailHisList.stream().map(FiPaymentVoucherDetail::getId).collect(Collectors.toList());
		List<String> delIdList = detailIdList;
		// 子表数据更新
		if(fiPaymentVoucherDetailList!=null && fiPaymentVoucherDetailList.size()>0) {
			for(FiPaymentVoucherDetail entity:fiPaymentVoucherDetailList) {
				BigDecimal verifyAmount = entity.getVerifyAmount() == null ? BigDecimal.ZERO : entity.getVerifyAmount();
				if(verifyAmount.compareTo(BigDecimal.ZERO) == -1){
					throw new RuntimeException("核销金额不能小于零");
				}
				entity.setMainId(fiPaymentVoucher.getId());
				//业务单据处理
				handleVoucherDetail(entity);
				//
				if(!detailIdList.contains(entity.getId())){
					//新增
					fiPaymentVoucherDetailMapper.insert(entity);
				}else{
					//修改
					fiPaymentVoucherDetailMapper.updateById(entity);
				}
			}
			//获取删除记录ID
			List<String> curIdList = fiPaymentVoucherDetailList.stream().map(FiPaymentVoucherDetail::getId).collect(Collectors.toList());
			delIdList = detailIdList.stream().filter(id -> !curIdList.contains(id)).collect(Collectors.toList());
		}
		// 删除记录处理
		if(oConvertUtils.listIsNotEmpty(delIdList)){
			for(String id : delIdList){
				// 业务单据处理
				FiPaymentVoucherDetail entityHis = fiPaymentVoucherDetailMapper.selectById(id);
				FiPaymentVoucherDetail entityDel = new FiPaymentVoucherDetail();
				entityDel.setId(id);
				entityDel.setBillId(entityHis.getBillId());
				entityDel.setBillType(entityHis.getBillType());
				entityDel.setVerifyAmount(BigDecimal.ZERO);
				handleVoucherDetail(entityDel);
				// 删除
				fiPaymentVoucherDetailMapper.deleteById(id);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		FiPaymentVoucher fiPaymentVoucher = fiPaymentVoucherMapper.selectById(id);
		if(!AparVoucherStatus.STATUS_01.equals(fiPaymentVoucher.getStatus())){
			throw new RuntimeException("已经审核，不能删除");
		}
		List<FiPaymentVoucherDetail> fiPaymentVoucherDetailList = fiPaymentVoucherDetailMapper.selectByMainId(id);
		if(oConvertUtils.listIsNotEmpty(fiPaymentVoucherDetailList)){
			for(FiPaymentVoucherDetail fiPaymentVoucherDetail: fiPaymentVoucherDetailList){
				//处理业务单据
				FiPaymentVoucherDetail detailDel = new FiPaymentVoucherDetail();
				detailDel.setId(fiPaymentVoucherDetail.getId());
				detailDel.setBillId(fiPaymentVoucherDetail.getBillId());
				detailDel.setBillType(fiPaymentVoucherDetail.getBillType());
				detailDel.setVerifyAmount(BigDecimal.ZERO);
				handleVoucherDetail(detailDel);
			}
		}
		//
		fiPaymentVoucherDetailMapper.deleteByMainId(id);
		fiPaymentVoucherMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			delMain(id.toString());
		}
	}

	@Override
	@Transactional
	public void audit(List<FiPaymentVoucher> fiPaymentVoucherList) {
		for(FiPaymentVoucher fiPaymentVoucher : fiPaymentVoucherList) {
			FiPaymentVoucher fiPaymentVoucherEntity = fiPaymentVoucherMapper.selectById(fiPaymentVoucher.getId());
			if (fiPaymentVoucherEntity == null) {
				throw new RuntimeException("未找到对应数据");
			}
			String status = fiPaymentVoucherEntity.getStatus();
			if (!AparVoucherStatus.STATUS_01.equals(status)) {
				throw new RuntimeException("已经审核，请不要重复操作");
			}
			BigDecimal verifyAmount = fiPaymentVoucherEntity.getVerifyAmount();
			if (PaymentVoucherType.TYPE_01.equals(fiPaymentVoucherEntity.getVoucherType())
					&& (verifyAmount == null || BigDecimal.ZERO.compareTo(verifyAmount) == 0)) {
				throw new RuntimeException("尚未进行核销，不能审核");
			}
			BigDecimal actAmount = fiPaymentVoucherEntity.getActAmount();
			if (actAmount == null || BigDecimal.ZERO.compareTo(actAmount) == 0 || actAmount.compareTo(verifyAmount) == -1) {
				throw new RuntimeException("实付金额有误，请修改数据");
			}
			// 审核
			FiPaymentVoucher fiPaymentVoucherAudit = new FiPaymentVoucher();
			fiPaymentVoucherAudit.setId(fiPaymentVoucher.getId());
			fiPaymentVoucherAudit.setAuditDate(new Date());
			fiPaymentVoucherAudit.setAuditFlag("1");
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			fiPaymentVoucherAudit.setAuditor(sysUser != null ? sysUser.getUsername() : null);
			if (PaymentVoucherType.TYPE_01.equals(fiPaymentVoucherEntity.getVoucherType())) {
				// 普通付款，审核同时核销
				fiPaymentVoucherAudit.setVerifyFlag("1");
				fiPaymentVoucherAudit.setVerifyDate(new Date());
				fiPaymentVoucherAudit.setVerifier(sysUser != null ? sysUser.getUsername() : null);
				fiPaymentVoucherAudit.setStatus(AparVoucherStatus.STATUS_03);
				fiPaymentVoucherAudit.setActAmount(verifyAmount);
			} else {
				// 预付款、直接付款，只审核
				fiPaymentVoucherAudit.setStatus(AparVoucherStatus.STATUS_02);
			}
			fiPaymentVoucherMapper.updateById(fiPaymentVoucherAudit);
			// 更新业务单据状态
			List<FiPaymentVoucherDetail> fiPaymentVoucherDetailList = fiPaymentVoucherDetailMapper.selectByMainId(fiPaymentVoucher.getId());
			for (FiPaymentVoucherDetail fiPaymentVoucherDetail : fiPaymentVoucherDetailList) {
				handleVoucherDetailStatus(fiPaymentVoucherDetail);
			}
		}
	}

	@Transactional
	@Override
	public void verify(List<FiPaymentVoucher> fiPaymentVoucherList) {
		for(FiPaymentVoucher fiPaymentVoucher : fiPaymentVoucherList) {
			FiPaymentVoucher fiPaymentVoucherEntity = fiPaymentVoucherMapper.selectById(fiPaymentVoucher.getId());
			if (fiPaymentVoucherEntity == null) {
				throw new RuntimeException("未找到对应数据");
			}
			String status = fiPaymentVoucherEntity.getStatus();
			if (!AparVoucherStatus.STATUS_02.equals(status)) {
				throw new RuntimeException("当前状态，不能进行核销");
			}
			BigDecimal verifyAmount = fiPaymentVoucherEntity.getVerifyAmount();
			if (verifyAmount == null || BigDecimal.ZERO.compareTo(verifyAmount) == 0) {
				throw new RuntimeException("请先维护核销金额");
			}
			BigDecimal actAmount = fiPaymentVoucherEntity.getActAmount();
			if (actAmount == null || BigDecimal.ZERO.compareTo(actAmount) == 0 || actAmount.compareTo(verifyAmount) != 1) {
				throw new RuntimeException("实付金额有误，请修改数据");
			}
			// 核销
			FiPaymentVoucher fiPaymentVoucherAudit = new FiPaymentVoucher();
			fiPaymentVoucherAudit.setId(fiPaymentVoucher.getId());
			fiPaymentVoucherAudit.setVerifyFlag("1");
			fiPaymentVoucherAudit.setVerifyDate(new Date());
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			fiPaymentVoucherAudit.setVerifier(sysUser != null ? sysUser.getUsername() : null);
			fiPaymentVoucherAudit.setStatus(AparVoucherStatus.STATUS_03);
			fiPaymentVoucherMapper.updateById(fiPaymentVoucherAudit);
			// 更新业务单据状态
			List<FiPaymentVoucherDetail> fiPaymentVoucherDetailList = fiPaymentVoucherDetailMapper.selectByMainId(fiPaymentVoucher.getId());
			for (FiPaymentVoucherDetail fiPaymentVoucherDetail : fiPaymentVoucherDetailList) {
				handleVoucherDetailStatus(fiPaymentVoucherDetail);
			}
		}
	}

	private void handleVoucherDetailStatus (FiPaymentVoucherDetail entity){
		if(AparVoucherDetailBillType.AP_01.equals(entity.getBillType())) {
			//采购入库单
			MmStockInOutDetail stockInOutDetail = mmStockInOutDetailMapper.selectById(entity.getBillId());
			BigDecimal sumPrice = stockInOutDetail.getSumPrice() == null ? BigDecimal.ZERO : stockInOutDetail.getSumPrice();
			BigDecimal stockVerifyAmount = stockInOutDetail.getVerifyAmount() == null ? BigDecimal.ZERO : stockInOutDetail.getVerifyAmount();
			if(stockVerifyAmount.compareTo(sumPrice) == 1){
				throw new RuntimeException("核销金额不能大于单据金额");
			}
			MmStockInOutDetail stockInOutDetailModify = new MmStockInOutDetail();
			stockInOutDetailModify.setId(stockInOutDetail.getId());
			if (stockVerifyAmount.compareTo(sumPrice) == 0) {
				stockInOutDetailModify.setVerifyStatus(StockInOutDetailVerifyStatus.STATUS_03);
			} else if (stockVerifyAmount.compareTo(BigDecimal.ZERO) == 1) {
				stockInOutDetailModify.setVerifyStatus(StockInOutDetailVerifyStatus.STATUS_02);
			} else {
				stockInOutDetailModify.setVerifyStatus(StockInOutDetailVerifyStatus.STATUS_01);
			}
			mmStockInOutDetailMapper.updateById(stockInOutDetailModify);
		}else if(AparVoucherDetailBillType.AP_04.equals(entity.getBillType())) {
			// 应付期初余额
			FiApOriginalAmount fiApOriginalAmount = fiApOriginalAmountMapper.selectById(entity.getBillId());
			BigDecimal initialBalance = fiApOriginalAmount.getInitialBalance() == null ? BigDecimal.ZERO : fiApOriginalAmount.getInitialBalance();
			BigDecimal originalVerifyAmount = fiApOriginalAmount.getVerifyAmount() == null ? BigDecimal.ZERO : fiApOriginalAmount.getVerifyAmount();
			if(originalVerifyAmount.compareTo(initialBalance) == 1){
				throw new RuntimeException("核销金额不能大于期初余额");
			}
			FiApOriginalAmount fiApOriginalAmountModify = new FiApOriginalAmount();
			fiApOriginalAmountModify.setId(fiApOriginalAmount.getId());
			if (originalVerifyAmount.compareTo(initialBalance) == 0) {
				fiApOriginalAmountModify.setSettleStatus(AparSettleStatus.STATUS_30);
			} else if (originalVerifyAmount.compareTo(BigDecimal.ZERO) == 1) {
				fiApOriginalAmountModify.setSettleStatus(AparSettleStatus.STATUS_20);
			} else {
				fiApOriginalAmountModify.setSettleStatus(AparSettleStatus.STATUS_10);
			}
			fiApOriginalAmountMapper.updateById(fiApOriginalAmountModify);
		}else if(AparVoucherDetailBillType.AP_03.equals(entity.getBillType())) {
			// 其他应付单
			FiOtherPaymentDetail fiOtherPaymentDetail = fiOtherPaymentDetailMapper.selectById(entity.getBillId());
			BigDecimal otherPaymentAmount = fiOtherPaymentDetail.getPaymentAmount() == null ? BigDecimal.ZERO : fiOtherPaymentDetail.getPaymentAmount();
			BigDecimal otherVerifyAmount = fiOtherPaymentDetail.getVerifyAmount() == null ? BigDecimal.ZERO : fiOtherPaymentDetail.getVerifyAmount();
			if(otherVerifyAmount.compareTo(otherPaymentAmount) == 1){
				throw new RuntimeException("核销金额不能大于单据金额");
			}
			FiOtherPaymentDetail fiOtherPaymentDetailModify = new FiOtherPaymentDetail();
			fiOtherPaymentDetailModify.setId(fiOtherPaymentDetail.getId());
			if (otherVerifyAmount.compareTo(otherPaymentAmount) == 0) {
				fiOtherPaymentDetailModify.setStatus(AparOtherDetailStatus.STATUS_03);
			} else if (otherVerifyAmount.compareTo(BigDecimal.ZERO) == 1) {
				fiOtherPaymentDetailModify.setStatus(AparOtherDetailStatus.STATUS_02);
			} else {
				fiOtherPaymentDetailModify.setStatus(AparOtherDetailStatus.STATUS_01);
			}
			fiOtherPaymentDetailMapper.updateById(fiOtherPaymentDetailModify);
			// 更新主表状态
			fiOtherPaymentMapper.autoUpdateStatus(fiOtherPaymentDetail.getMainId());
		}
	}

	/**
	 * 明细分页查询
	 * @param page
	 * @param param
	 * @return
	 */
	@Override
	public void detailPage(Page<FiPaymentVoucherDetailModel> page, FiPaymentVoucherDetailPage param) {
		page.setRecords(fiPaymentVoucherDetailMapper.selectDetailList(page, param, null));
	}

	/**
	 * 供应商应付明细账-付款
	 * @param page
	 * @param param
	 */
	@Override
	public void supplyPaymentDetailPage(Page<FiSupplyPaymentDetailModel> page, FiSupplyPaymentDetailPage param) {
		page.setRecords(fiPaymentVoucherDetailMapper.selectSupplyPaymentList(page, param, null));
	}

	/**
	 * 供应商应付明细账-预付款
	 * @param page
	 * @param param
	 */
	@Override
	public void supplyPrePaymentDetailPage(Page<FiSupplyPrePaymentDetailModel> page, FiSupplyPaymentDetailPage param) {
		page.setRecords(fiPaymentVoucherDetailMapper.selectSupplyPrePaymentList(page, param, null));
	}
}
